package org.project.s.controller;


import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.project.pojo.Download;
import org.project.pojo.Download0;
import org.project.pojo.Fengye;
import org.project.pojo.Order;
import org.project.pojo.Showorderdetail;
import org.project.pojo.TypeDetail;
import org.project.service.user.Order1Service;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/order1controller")
public class Order1controller {
   @Resource
   private Order1Service order1Service;
   
   @RequestMapping("/selectwc")
   public String selectwc(HttpServletRequest request, HttpServletResponse response){
	   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	
		
		String str=request.getParameter("n");   
//		System.out.println(str);
		Fengye fengye=new Fengye();
	     //总行数  
	     int lineCount=order1Service.selectwccount();
	     //每页的行数  
	     int pageSize=10;  
	     //总共的页数  
	     int pageCount=(int)Math.ceil(lineCount*1.0/pageSize);  
	       
	     int n = 0;  
	               
	     if(str == null || str.equals("")){  
	         n = 0;  
	     }else if(str.equals("a")){  
	    	 
	          n = (Integer)request.getSession().getAttribute("pageNow");  
	         n=n-pageSize;  
	     }else if(str.equals("b")){  
	          n = (Integer)request.getSession().getAttribute("pageNow");  
	         n=n+pageSize;  
	     }else if(str.equals("0")){  
	         n=0;  
	     }else{  
	         n=(pageCount-1)*pageSize;  
	     }     
	       
	     if(n>((pageCount-1)*pageSize)){  
	         n=(pageCount-1)*pageSize;  
	     }  
	     if(n<0){  
	         n=0;  
	     }     
	       
	       
	       
	     request.getSession().setAttribute("pageNow", n);  
	     request.getSession().setAttribute("pageCount", pageCount);    
	       
	   
	   /*  System.out.println(n);
	     System.out.println(pageSize);*/
	     fengye.setN(n);
	     fengye.setPageSize(pageSize);
	     List<Order> orderlist= order1Service.selectwc(fengye);
	    
	    
		 
		       req.setAttribute("orderlist", orderlist);
		       req.setAttribute("wccount", order1Service.selectwccount());
	          
		       return "showwcorder";
   }
   
   
   
   
   @RequestMapping("/payyue")
   public String payyue(@Param("orderid")String orderid){
	   int i=order1Service.payyue(orderid);
	   if(i>0){
		   return "redirect:/order1controller/selectyewzf";
	   }else{
		   
		   return "";
	   }
	   
   }
   
   
   
   
   
   
   @RequestMapping("/selectyewzf")
   public String selectyewzf(HttpServletRequest request, HttpServletResponse response){
	   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		
		
		String str=request.getParameter("n");   
//		System.out.println(str);
		Fengye fengye=new Fengye();
	     //总行数  
	     int lineCount=order1Service.selectyewzfcount();
	     //每页的行数  
	     int pageSize=10;  
	     //总共的页数  
	     int pageCount=(int)Math.ceil(lineCount*1.0/pageSize);  
	       
	     int n = 0;  
	               
	     if(str == null || str.equals("")){  
	         n = 0;  
	     }else if(str.equals("a")){  
	    	 
	          n = (Integer)request.getSession().getAttribute("pageNow");  
	         n=n-pageSize;  
	     }else if(str.equals("b")){  
	          n = (Integer)request.getSession().getAttribute("pageNow");  
	         n=n+pageSize;  
	     }else if(str.equals("0")){  
	         n=0;  
	     }else{  
	         n=(pageCount-1)*pageSize;  
	     }     
	       
	     if(n>((pageCount-1)*pageSize)){  
	         n=(pageCount-1)*pageSize;  
	     }  
	     if(n<0){  
	         n=0;  
	     }     
	       
	       
	     request.getSession().setAttribute("pageNow", n);  
	     request.getSession().setAttribute("pageCount", pageCount);    
	       
	   
	   /*  System.out.println(n);
	     System.out.println(pageSize);*/
	     fengye.setN(n);
	     fengye.setPageSize(pageSize);
	     List<Order> orderlist= order1Service.selectyewzf(fengye);
	    
	    
		 
		       req.setAttribute("orderlist", orderlist);
		       req.setAttribute("yewzfcount", order1Service.selectyewzfcount());
	          
		     	   
	    
	   
	   return "showyewzforder";
   }
   
   
   
   
   @RequestMapping("/selectwzf2")
   public String selectwzf2(HttpServletRequest request, HttpServletResponse response){
	   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		
		
		String str=request.getParameter("n");   
//		System.out.println(str);
		Fengye fengye=new Fengye();
	     //总行数  
	     int lineCount=order1Service.selectwzf2count();
	     //每页的行数  
	     int pageSize=10;  
	     //总共的页数  
	     int pageCount=(int)Math.ceil(lineCount*1.0/pageSize);  
	       
	     int n = 0;  
	               
	     if(str == null || str.equals("")){  
	         n = 0;  
	     }else if(str.equals("a")){  
	    	 
	          n = (Integer)request.getSession().getAttribute("pageNow");  
	         n=n-pageSize;  
	     }else if(str.equals("b")){  
	          n = (Integer)request.getSession().getAttribute("pageNow");  
	         n=n+pageSize;  
	     }else if(str.equals("0")){  
	         n=0;  
	     }else{  
	         n=(pageCount-1)*pageSize;  
	     }     
	       
	     if(n>((pageCount-1)*pageSize)){  
	         n=(pageCount-1)*pageSize;  
	     }  
	     if(n<0){  
	         n=0;  
	     }     
	       
	       
	       
	     request.getSession().setAttribute("pageNow", n);  
	     request.getSession().setAttribute("pageCount", pageCount);    
	       
	   
	   /*  System.out.println(n);
	     System.out.println(pageSize);*/
	     fengye.setN(n);
	     fengye.setPageSize(pageSize);
	     List<Order> orderlist= order1Service.selectwzf2(fengye);
	    
	    System.out.println(orderlist);
		 
		       req.setAttribute("orderlist", orderlist);
		       req.setAttribute("wzf2count", order1Service.selectwzf2count());
	          
		     	   
	   
	   
	   
	   
	   
	   return "showwzf2order";
   }
   
   
   
   
   
   
   @RequestMapping("/selectyqx")
   public String selectyqx(HttpServletRequest request, HttpServletResponse response){
	   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		
		
		String str=request.getParameter("n");   
//		System.out.println(str);
		Fengye fengye=new Fengye();
	     //总行数  
	     int lineCount=order1Service.selectyqxcount();
	     //每页的行数  
	     int pageSize=10;  
	     //总共的页数  
	     int pageCount=(int)Math.ceil(lineCount*1.0/pageSize);  
	       
	     int n = 0;  
	               
	     if(str == null || str.equals("")){  
	         n = 0;  
	     }else if(str.equals("a")){  
	    	 
	          n = (Integer)request.getSession().getAttribute("pageNow");  
	         n=n-pageSize;  
	     }else if(str.equals("b")){  
	          n = (Integer)request.getSession().getAttribute("pageNow");  
	         n=n+pageSize;  
	     }else if(str.equals("0")){  
	         n=0;  
	     }else{  
	         n=(pageCount-1)*pageSize;  
	     }     
	       
	     if(n>((pageCount-1)*pageSize)){  
	         n=(pageCount-1)*pageSize;  
	     }  
	     if(n<0){  
	         n=0;  
	     }     
	       
	       
	     request.getSession().setAttribute("pageNow", n);  
	     request.getSession().setAttribute("pageCount", pageCount);    
	       
	   
	   /*  System.out.println(n);
	     System.out.println(pageSize);*/
	     fengye.setN(n);
	     fengye.setPageSize(pageSize);
	     List<Order> orderlist= order1Service.selectyqx(fengye);
	    
	    
		 
		       req.setAttribute("orderlist", orderlist);
		       req.setAttribute("yqxcount", order1Service.selectyqxcount());
	          
		
	   return "showyqxorder";
   }
   
   
   
   
  	@RequestMapping("/showsxorders")
   public String showsxorders(HttpServletRequest request, HttpServletResponse response){
   	 HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		
		
		String str=request.getParameter("n");   
//		System.out.println(str);
		Fengye fengye=new Fengye();
	     //总行数  
	     int lineCount=order1Service.selectsxcount();
	     //每页的行数  
	     int pageSize=10;  
	     //总共的页数  
	     int pageCount=(int)Math.ceil(lineCount*1.0/pageSize);  
	       
	     int n = 0;  
	               
	     if(str == null || str.equals("")){  
	         n = 0;  
	     }else if(str.equals("a")){  
	    	 
	          n = (Integer)request.getSession().getAttribute("pageNow");  
	         n=n-pageSize;  
	     }else if(str.equals("b")){  
	          n = (Integer)request.getSession().getAttribute("pageNow");  
	         n=n+pageSize;  
	     }else if(str.equals("0")){  
	         n=0;  
	     }else{  
	         n=(pageCount-1)*pageSize;  
	     }     
	       
	     if(n>((pageCount-1)*pageSize)){  
	         n=(pageCount-1)*pageSize;  
	     }  
	     if(n<0){  
	         n=0;  
	     }     
	       
	       
	     request.getSession().setAttribute("pageNow", n);  
	     request.getSession().setAttribute("pageCount", pageCount);    
	       
	   
	     System.out.println(n);
	     System.out.println(pageSize);
	     fengye.setN(n);
	     fengye.setPageSize(pageSize);
   		
	     List<Order> orderlist= order1Service.selectsx(fengye);
		    
		    
		 
	       req.setAttribute("orderlist", orderlist);
	       req.setAttribute("count", order1Service.selectsxcount());
           req.setAttribute("ytkcount",order1Service.selectytkcount());
           req.setAttribute("wtkcount",order1Service.selectwtkcount());
           
           
           return "showsxorder";
	     
	     
	     
	     
   		
   	}
   
   
   
   
   
	   @RequestMapping("/deleteorder")
	   public String deleteorder(@Param("orderid")String orderid){
		   
		   int i=order1Service.deleteorder(orderid);
		   
		   if(i>0){
			   return "mainfra";
		   }else{
			   return "";
		   }
	   } 
   
	
	  
	   @RequestMapping("/dotuikuan")
	   public String dotuikuan(@Param("orderid")String orderid){
		   order1Service.tuikuan(orderid);
		   
		  return "redirect:/order1controller/showsxorders";
		   
		   
	   }
	   
	   
	   @RequestMapping("/showdetail")
	   public String showdetail(@Param("orderid")String orderid){
		   HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		   
		   Order order=order1Service.selectorder1(orderid);
		   List<TypeDetail> list=order1Service.selecttypes(order);
		   Showorderdetail showorderdetail=new Showorderdetail();
		   showorderdetail.setOrderid(order.getOrderid());
		   showorderdetail.setOrderTime(order.getOrderTime());
		   showorderdetail.setShopName(order.getShopName());
		   showorderdetail.setPrice(order.getPrice());
		   showorderdetail.setPrepayPrice(order.getPrepayPrice());
		   showorderdetail.setTypedetaillist(list);
		   req.setAttribute("showorderdetail", showorderdetail);
		   
		   return"showorderdetails";
	   }
   
   
	   
	   
	   
   @RequestMapping("/showdownload")
   public String seephoto(@Param("orderid")String orderid){

	   List<Download0>list=order1Service.seephoto(orderid);
		HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		String path=getLoaclURL(req)+"download//";
		List<Download> list2=new ArrayList<Download>();
		if(list!=null){
   		Iterator<Download0> it = list.iterator();
   		while(it.hasNext()) {
   			Download0 download0=it.next();
   			Download download=new Download();
   			download.setId(download0.getId());
   			download.setOrderid(download0.getOrderid());
   			download.setUrl(path+download0.getImgName());
   			download.setUploadTime(download0.getUploadTime());
   			list2.add(download);
   		}
   		}
		    System.out.println(list2+"aaaddddccc");
		    System.out.println(orderid+"fuhzu");
	        req.setAttribute("list2", list2);
	        req.setAttribute("orderid", orderid);
	   
	      return "seephoto";
   }
	   
   
   
   
   
  
   
  @RequestMapping("/uploadphotodo")
	   public String uploadphotodo(@Param("orderid")String orderid,@ RequestParam MultipartFile items_pic) throws IOException{
	  HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		String originalFilename = items_pic.getOriginalFilename();
		 Download0 download0=null;
		if (items_pic!=null && originalFilename!=null && originalFilename.length()>0) {
		String pic_path=getRealPath(req)+"download//";
		//新图片名称
		
		String newFileName=UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
 
		 File newFile=new File(pic_path+newFileName); 
	     
		    download0=new Download0();
			download0.setOrderid(orderid);
			download0.setImgName(newFileName);
			Date date = new Date();
			SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String uploadtime = sim.format(date);
			download0.setUploadTime(uploadtime);
	
			
		//00开始读图片
		 try {
				items_pic.transferTo(newFile);
				
				
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		    String subimgName=createThumbPic(newFile,pic_path);	
		    download0.setSubimgName(subimgName);
		 
		 
				}
	    
		
		
		
		
		
		
		
		
		int i=order1Service.uploadphoto(download0);
		
		
		
		
		
		
		
		
		
		
		
		
		
		if(i>0){
			return "redirect:/order1controller/showdownload?orderid="+orderid;
		}else{
			return  "redirect:/order1controller/showdownload?orderid="+orderid;
			
		}  
	  
	  
   }
	   
	
  
  
  /**
  * 压缩图片 文件名为原图名_sub
  * 
  * @param fileName
  * @return
  * @throws IOException
  */
  public static String createThumbPic(File file,String realPath) throws IOException {
  // java.io.File pFile = new java.io.File(file);

  String path = realPath+"/"+file.getName().replace(".", "_sub.");
  String fileName = file.getName();
  java.io.File fo = new java.io.File(path); // 将要转换出的小图文件
  int nw = 1000;
  AffineTransform transform = new AffineTransform();
  BufferedImage buffer = ImageIO.read(file); // 读取图片
  int width = buffer.getWidth();
  int height = buffer.getHeight();
  int nh = (nw * height) / width;
  double sx = (double) nw / width;
  double sy = (double) nh / height;
  transform.setToScale(sx, sy);
  AffineTransformOp ato = new AffineTransformOp(transform, null);
  BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
  ato.filter(buffer, bid);
  String type = fileName.substring(fileName.indexOf('.') + 1, fileName.length());
  // System.out.println("type = " + type);
  ImageIO.write(bid, type, fo);
  return fo.getName();
  }
  

  
  
  @RequestMapping("/deleteuploadphoto")
  public String deleteuploadphoto(@Param("id")int id){
	  Download0 download0=new Download0();
	  download0.setId(id);
	  Download0 download1=order1Service.selectbyid(download0);
	
	  HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	  	  
	  String pic_path=getRealPath(req)+"download//";
	  String imgName=download1.getImgName();
	  
	  File file=new File(pic_path+imgName);                     
	  boolean b=file.delete();                                 //删除真实图片
	  int i=order1Service.deletebyid(download0);               //删除数据库记录
	  if(b==true&&i>0){
		  return "redirect:/order1controller/showdownload?orderid="+download1.getOrderid();
	  }else{
		  
		  return "redirect:/order1controller/showdownload?orderid="+download1.getOrderid();
		  
	  }
	 }
	  
  
  
  
  
  
  @RequestMapping("/deletewcorder")
  public String deletewcorder(@Param("orderid")String orderid){
	
	  HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	  String pic_path=getRealPath(req)+"download//";
 	   order1Service.deleteorder(orderid);                  //删除订单
	   order1Service.deletebyorderid(orderid);              //删除数据库中上传的图片记录
 	  
	   List<Download0> list=order1Service.seephoto(orderid);       //通过orderid得到表中的上传的图片的集合
	   if(null!=list){                                             
	   Iterator<Download0>it=list.iterator();                      
	   while(it.hasNext()) {                                       
		   Download0 download0=it.next();                           
		   File file=new File(pic_path+download0.getImgName());
           file.delete();		
	}
  }
        return   "redirect:/order1controller/selectwc";
  }
  
  
  
  
  
  
  
  public static String getRealPath(HttpServletRequest request) {

		return request.getSession().getServletContext().getRealPath("/");
	}

   
  
  
  
   public static String getLoaclURL(HttpServletRequest request) {
		
		String path_now = request.getContextPath();
		return request.getScheme() + "://" + request.getServerName() + ":"
				+ request.getServerPort() + path_now + "/";
	}
}
